package com.xh.pmo.actions;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.xh.pmo.base.ModelDrivenBaseAction;
import com.xh.pmo.domain.Team;
import com.xh.pmo.domain.Team_User;
import com.xh.pmo.domain.User;

@Controller
@Scope("prototype")
public class TeamAction extends ModelDrivenBaseAction<Team> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2520087838086515340L;

	private File upload;
	private Long teamId;
	private Long userId;
	
	private String emails;
	
	public String edit () {
		System.out.println(model.getId());
		
		Team team = teamService.getById(model.getId());
		team.setName(model.getName());
		team.setLinkman(model.getLinkman());
		team.setPhoneNumber(model.getPhoneNumber());
		team.setAddress(model.getAddress());
		if(upload != null) {
			String fileName = uplaod(upload);
			System.out.println(fileName);
			team.setLog(fileName);
		}
		teamService.update(team);
		return "toMyTeam";
	}
	
	public String list () {
		User user = getCurrentUser();
		List<Team_User> teamUserList = team_userService.findTeamByUser(user);
		ActionContext.getContext().put("teamUserList", teamUserList);
		Team_User tu = teamUserList.get(0);
		System.out.println("团队 联系对象" + tu.getTeam().getLinkman());
		System.out.println("团队数量" + teamUserList.size());
		return "list";
	}
	
	public String setUserAdmin () {
		System.out.println(teamId + "--" + userId);
		teamService.setUserAdmin(teamId,userId);
		return "toMyTeam";
	} 
	
	public String myTeam (){
		Team team = teamService.getTeamByUser(getCurrentUser());
		ActionContext.getContext().put("team", team);
		List<Team_User> teamUserList =	team_userService.getTeamUserByTeam(team);
		ActionContext.getContext().put("teamUserList", teamUserList);
		return "myTeam";
	}
	
	public String removeUser() {
		System.out.println(teamId + "--" + userId);
		User user = userService.getById(userId);
		user.setTeamCount(user.getTeamCount() - 1);
		userService.update(user);
		
		
		teamService.removeUser(teamId,userId);
		return "toMyTeam";
	}
	
	private String uplaod (File upload) {
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy_MM_DD_HH_mm_ss");
		String basePath  = ServletActionContext.getServletContext().getRealPath("/img/log/");
		File file = new File(basePath);
		if(!file.exists()) {
			file.mkdirs();
		}
		
		String fileName = simpleDate.format(new Date()) + UUID.randomUUID().toString();
		String path  = basePath + "/" + fileName;
		
		upload.renameTo(new File(path));
		return fileName;
	}
	
	public static void main(String[] args) {
		String str = "535737953@qq.com\n535737953@qq.com";
		System.out.println(str);
		
		String[] strs = str.split("\n");
		System.out.println(strs[0] + "  " + strs[1]);
	}
	public String addUserForTeam () {
		
		String[] newEmails = emails.split("\n");
		System.out.println(newEmails.length);
		
		for(int i=0; i<newEmails.length; i++) {
			newEmails[i] = newEmails[i].trim();
		}
		
		List<User> users = userService.findByEmails(newEmails);
		
		System.out.println(users.size() + "   ===");
		
		
		Team team = teamService.getById(teamId);
		team_userService.save(users,team);
		return "toMyTeam";
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public Long getTeamId() {
		return teamId;
	}

	public void setTeamId(Long teamId) {
		this.teamId = teamId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getEmails() {
		return emails;
	}

	public void setEmails(String emails) {
		this.emails = emails;
	}



	
	
	
}
